package com.elong.android.specialhouse.activity;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.entity.HouseImageItem;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.List;

/**
 * @author cheng.wang
 */
public class ImageBrowseAdapter extends PagerAdapter {

    private Context mContext;
    private ImageLoader imageLoader;
    private DisplayImageOptions options;
    private ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    private List<HouseImageItem> mImageList;

    public ImageBrowseAdapter(Context context, List<HouseImageItem> imageList) {
        imageLoader = ImageLoader.getInstance();
        options = new DisplayImageOptions.Builder().showStubImage(R.drawable.bg_loading_default).showImageForEmptyUri(R.drawable.bg_loading_default)
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED).cacheInMemory().cacheOnDisc().build();
        this.mContext = context;
        this.mImageList = imageList;
    }

    @Override
    public int getCount() {
        return null == mImageList ? 0 : mImageList.size();
    }


    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(mContext);
        container.addView(imageView, layoutParams);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        HouseImageItem item;
        if (null == mImageList) {
            imageView.setImageResource(R.drawable.bg_loading_default);
        }else {
            item = mImageList.get(position);
            imageLoader.displayImage(item.ImagePath, imageView, options);
        }
        return imageView;
    }

}
